package com.zxl.diy.weather.model.bean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 城市天气信息单元
 * Created by Administrator on 2016/11/6.
 */
public class Weather {

    public String weatherCityId;

    public long updateTime;

    /**
     * 当前城市信息
     */
    public City city;

    /**
     * 当前天气状况
     */
    public Now now;

    /**
     * 当天气象指数
     */
    public ZhiShu zhiShu;

    /**
     * 天气预报
     */
    public List<Forcast> forcasts;

    /**
     *  24小时天气状况
     */
    public List<Hourly> hourlys;

    /**
     * 极端天气预警
     */
    public List<Alarm> alarms;

    /**
     * 空气质量
     */
    public Aqi aqi;

    /**
     * 花粉指数
     */
    public List<PollenIndex> pollenIndices;

    /**
     * 雷达
     */
    public Radar radar;

    /**
     * 生活指数
     */
    public WeatherIndices weatherIndices;


    public void parseJson(JSONObject jsonObject) {
        weatherCityId = jsonObject.optString("weatherCityId");

        updateTime = jsonObject.optLong("updateTime");

        JSONObject cityJsonObject = jsonObject.optJSONObject("city");
        if (null != cityJsonObject) {
            city = new City();
            city.parseJson(cityJsonObject);
        }

        JSONObject nowJsonObject = jsonObject.optJSONObject("currentWeather");
        if (null != nowJsonObject) {
            now = new Now();
            now.parseJson(nowJsonObject);
        }

        JSONObject zhiShuJsonObject = jsonObject.optJSONObject("zhishu");
        if (null != zhiShuJsonObject) {
            zhiShu = new ZhiShu();
            zhiShu.parseJson(zhiShuJsonObject);
        }

        JSONArray forcastJsonArray = jsonObject.optJSONArray("forecasts");
        if (null != forcastJsonArray && forcastJsonArray.length() > 0) {
            forcasts = new ArrayList<>();
            for (int i = 0; i < forcastJsonArray.length(); i++) {
                Forcast forcast = new Forcast();
                forcast.parseJson(forcastJsonArray.optJSONObject(i));
                forcasts.add(forcast);
            }
        }

        JSONArray hourlyJsonArray = jsonObject.optJSONArray("hourlies");
        if (null != hourlyJsonArray && hourlyJsonArray.length() > 0) {
            hourlys = new ArrayList<>();
            for (int i = 0; i < hourlyJsonArray.length(); i++) {
                Hourly hourly = new Hourly();
                hourly.parseJson(hourlyJsonArray.optJSONObject(i));
                hourlys.add(hourly);
            }
        }

        JSONArray alarmJsonArray = jsonObject.optJSONArray("alarms");
        if (null != alarmJsonArray && alarmJsonArray.length() > 0) {
            alarms = new ArrayList<>();
            for (int i = 0; i < alarmJsonArray.length(); i++) {
                Alarm alarm = new Alarm();
                alarm.parseJson(alarmJsonArray.optJSONObject(i));
                alarms.add(alarm);
            }
        }

        JSONObject aqiJsonObject = jsonObject.optJSONObject("aqi");
        if (null != aqiJsonObject) {
            aqi = new Aqi();
            aqi.parseJson(aqiJsonObject);
        }

        JSONArray pollenIndexJsonArray = jsonObject.optJSONArray("pollenIndex");
        if (null != pollenIndexJsonArray && pollenIndexJsonArray.length() > 0) {
            pollenIndices = new ArrayList<>();
            for (int i = 0; i < pollenIndexJsonArray.length(); i++) {
                PollenIndex pollenIndex = new PollenIndex();
                pollenIndex.parseJson(pollenIndexJsonArray.optJSONObject(i));
                pollenIndices.add(pollenIndex);
            }
        }

        JSONObject radarJsonObject = jsonObject.optJSONObject("radar");
        if (null != radarJsonObject) {
            radar = new Radar();
            radar.parseJson(radarJsonObject);
        }

        JSONObject weatherIndicesJsonObject = jsonObject.optJSONObject("weatherIndices");
        if (null != weatherIndicesJsonObject) {
            weatherIndices = new WeatherIndices();
            weatherIndices.parseJson(weatherIndicesJsonObject);
        }

    }

}
